package org.argeproje.resim.proc.view;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;

import org.argeproje.resim.ResimProjectEditor;
import org.argeproje.resim.ViewerPage;
import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ExtremaDA;
import org.argeproje.resim.proc.data.HistogramDA;
import org.argeproje.resim.proc.data.ImageDA;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.sun.media.jai.widget.DisplayJAI;

public class ViewerPR extends Processor implements MouseListener,
		MouseMotionListener {
	private String _title = "Figure"; 
	private int _mousePressX, _mousePressY, _mouseDragX, _mouseDragY;
	private int _viewEvent = 0;
	private static int VIEW_EVENT_PAN = 1;
	private static int VIEW_EVENT_ZOOM = 2;
	protected ArrayList<JPanel> _viewPanelList = new ArrayList<JPanel>();
	private int _mouseButton = 0;
	public void setParameters(String title){
		setParamTitle(title);
	}
    public void setParamTitle(String title){
    	_title = title;
    }
    public String getParamTitle(){
        return _title;
    }
	protected void setConnectionRules(ConnectionRule connRule) {
		connRule.addInType(HistogramDA.class, 0);
		connRule.addInType(ImageDA.class, 0);
		connRule.addInType(ExtremaDA.class, 0);
	}

	public Data process() {
		Processor pr = getProcessor();
		pr.process();
		return null;
	}

	protected ResimProjectEditor getProjectEditor() {
		return ResimProjectEditor.getActiveProjectEditor();
	}

	public JPanel getViewPanel(int index) {
		return _viewPanelList.get(index);
	}

	protected void setViewPanel(JPanel panel, String id, String title) {
		setViewPanel(panel, 0, id, title);
	}

	protected void setViewPanel(JPanel panel, int panelIndex, String id,
			String title) {
		panel.addMouseMotionListener(this);
		panel.addMouseListener(this);
		_viewPanelList.add(panel);
		ResimProjectEditor rpe = getProjectEditor();
		ViewerPage viewer = new ViewerPage(rpe, this, panelIndex, id, title);
		rpe.addPage(viewer, SWT.CLOSE);
	}

	private Processor getProcessor() {
		Processor pr = null;
		Data input = getInput();
		if (input.isOfType(Data.IMAGE)) {
			pr = new ImgViewerPR();
		} else if (input.isOfType(Data.HISTOGRAM)) {
			pr = new HistogramViewerPR();
		} else if (input.isOfType(Data.EXTREMA)) {
			pr = new ExtremaViewerPR();
		}
		((ViewerPR)pr).setParameters(getParamTitle());
		pr.setInput(input);
		return pr;
	}

	protected JPanel getShowingPanel() {
		ResimProjectEditor editor = ResimProjectEditor.getActiveProjectEditor();
		
		if(editor.getActivePageInstance() instanceof ViewerPage)
		{
			ViewerPage activePage = (ViewerPage)editor.getActivePageInstance();
			return _viewPanelList.get(activePage.getPanelIndex());
		}
		return null;
	}

	public void zoomIn() {
	}
	public void zoomDefault() {
	}
	public void zoomOut() {
	}
    protected void zoomRectangle(int x, int y, int width, int height){
    	
    }
	public void save(String fileName) {
	}

	/*
	 * Translates view origin by x and y pixels
	 */
	protected void translateOrigin(int x, int y) {

	}

	public void mouseDragged(MouseEvent e) {
		int mouseButton = getMouseButton();
		int x = e.getX();
		int y = e.getY();
		int mousePressX = getMousePressX();
		int mousePressY = getMousePressY();
		if (mouseButton == 1/* Pan */) {
			setViewEvent(VIEW_EVENT_PAN);
			_viewPanelList.get(0).setCursor(new Cursor(Cursor.HAND_CURSOR));
			translateOrigin(x - mousePressX, y - mousePressY);
			setMousePressX(e.getX());
			setMousePressY(e.getY());
		} else/* Draw rectangle */{
			setViewEvent(VIEW_EVENT_ZOOM);
			int width = x - mousePressX;
			int height = y - mousePressY;
			drawRect(mousePressX, mousePressY, width, height);
		}
	}

	public void drawRect(int x, int y, int width, int height) {

	}

	public void mouseMoved(MouseEvent e) {

		// TODO Auto-generated method stub

	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {
		setMouseButton(e.getButton());
		setMousePressX(e.getX());
		setMousePressY(e.getY());
	}

	public void mouseReleased(MouseEvent e) {
		if(_viewEvent == VIEW_EVENT_ZOOM){
			int x = e.getX();
			int y = e.getY();
			int mousePressX = getMousePressX();
			int mousePressY = getMousePressY();
			int width = x - mousePressX;
			int height = y - mousePressY;
			zoomRectangle(mousePressX, mousePressY, width, height);
		}
		_viewPanelList.get(0).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}

	protected void setMouseButton(int mouseButton) {
		_mouseButton = mouseButton;
	}

	protected int getMouseButton() {
		return _mouseButton;
	}

	protected void setMousePressX(int mousePressX) {
		_mousePressX = mousePressX;
	}

	protected int getMousePressX() {
		return _mousePressX;
	}

	protected void setMousePressY(int mousePressY) {
		_mousePressY = mousePressY;
	}

	protected int getMousePressY() {
		return _mousePressY;
	}
	private void setViewEvent(int viewEvent){
		_viewEvent = viewEvent;
	}
}
